<?
if($check_me_authed == "OK"){
	
	$today_date				= date("d-m-Y");
	
	$srcDbSequence      = "MST_SEQUENCE";
	$srcDbHdr			      = "SLS_RET_HDR";
  $srcDbHdrLog	      = "LOG_SLS_RET_HDR";
  $srcDbDtl           = "SLS_RET_DTL";
  $dstDbHistory       = "MODEL_HISTORY";
  $srcDbInputHistory  = "MODEL_INPUT_HISTORY";
  $srcDbManInHdr      = "SLS_RET_MANUAL_SER_NUMBER_HDR";
  $srcDbManInDtl      = "SLS_RET_MANUAL_SER_NUMBER_DTL";
	$srcDbModelHistory  = "MODEL_HISTORY";
	$srcDbSrcHdr        = "SLS_HDR";
	$srcDbSrcDtl        = "SLS_DTL";
	$srcDbMstWarehouse  = "MST_WAREHOUSE";
	
	if($_POST["selbox"] != ''){
		
		$UID     = unserialize($_SESSION["serUID"]);
    $userId  = $UID->getUID();
    
    $COracle = new oracleDb;
    $COracle->init($_SESSION["ORCL"], 
                   $_SESSION["OPASS"],
                   $_SESSION["SID"],
                   _NO_COMMENT);
    
    if($COracle->connect()){
			
			if(sizeof($_POST["selbox"]) == 0){
				
				usrDie("Error: Nothing to approve");
			}else{
				
				foreach($_POST["selbox"] as $apprThisJobId){
					
					/**
      		* Fetch global header
      		* And check the status if it is valid
      		* where STATUS = _NOT_ACTIVE 
      		*
      		*/
      		$CFetchers->fetchLstDataHeader($COracle,
                                         $apprThisJobId,
                                         $srcDbHdr,
                                         $nHeader,
                                         $lstHeader);
					
				  if($lstHeader["STATUS"][0] == _NOT_ACTIVE){
				    			      
			      /**
			      * Build up the REFERENCE number
			      *
			      */
			      $thisPeriode    = &$_SESSION["sysPeriode"];
			      
			      $seqEditStatus  = false;
            
            $thisSequence   = "SLS_REF_SEQ";
            
          	while($seqEditStatus == false){
          	  
            	$CFetchers->fetchCurSequence($COracle,
                          	               $thisSequence,
                          	               $thisPeriode,
                          	               $srcDbSequence,
                        	                 $srcJidIndex);
            	
            	$CHandlers->initCurSequence($COracle,
                          	              $thisSequence,
                          	              $srcJidIndex,
                          	              $thisPeriode,
                          	              $srcDbSequence,
                          	              $seqEditStatus);
            }
            
            /**
            * Extract the system period
            *
            */
            $tmpDate = explode("-", $thisPeriode);
            $wrkDate = $tmpDate[0]. /* This period month */
                       "-".
                       $tmpDate[1]; /* This period year */
            
            $refId  = addDigit((string) $srcJidIndex). 
                      $srcJidIndex. 
                      "/" . 
                      $wrkDate;
                      
            $CHandlers->procApproval($COracle,
                                     $apprThisJobId,
                                     $refId,
                                     $userId,
                                     $srcDbHdr);
            
            /**
            * Extra work is to input A/P and 
            * check the serial number of 
            * each tx and do input into inventory
            *
            */
            $thisJobId = $apprThisJobId;
            $isApprove = true;
            
            /**
            * Fetch the source date
            * since it is so significant
            *
            */
            fetchTxHeader($COracle,
                          $thisJobId,
                          $srcDbHdr,
                          $resHeader);
            
            $srcDate    = date("d-m-Y", 
                               strtotime($resHeader["TR_DATE"][0]));
            
            require("controllers/processInputHistory.php");
            require("controllers/processInventory.php");
            
            /**
            * Insert Log
            *
            */
            $CHandlers->insToDbLog($COracle,
                                   $today_date,
                                   $apprThisJobId,
                                   $userId,
                                   $_SERVER["REMOTE_ADDR"],
                                   _APPROVE_MODE,
                                   $srcDbHdrLog);    

				  }else{
				    
				    usrDie("Error: Tx is ACTIVE/APPROVED/CANCELED");
				  }
					
				}
					
				//$COracle->commit();
				
				if($COracle->getStatus() == true){
  
      	  usrMsg("Approval Complete");
      	}else{
      	  
      	  usrMsg("Approval Process Interupted");
      	}
      	
      	$COracle->dc();
				
			}
		}else{
			
			usrDie("Error: oracle connection failed");
		}
	}else{
		
		usrDie("Error: Unknown selection or bad ID retrieved");
		//jumpTo(2,"../".$_SESSION["source_caller"]);
	}
}else{
	
	usrDie("NOT AUTHED");
}
?>